package com.jfinal.club._admin.banji;

import com.jfinal.aop.Inject;
import com.jfinal.club._admin.college.CollegeAdminService;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Banji;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

public class BanjiAdminController extends BaseController{

	@Inject
	BanjiAdminService srv;
	@Inject
	CollegeAdminService cosrv;
	
	public void index() {
		Page<Banji> banjiPage = srv.paginate(getParaToInt("p", 1));
		setAttr("banjiPage", banjiPage);
		render("index.html");
	}
	
	public void add() {
		setAttr("collegeList", cosrv.getAll());
		render("add_edit.html");
	}
	
	//@Before(MyProjectValidator.class)
	public void save() {
		Banji banji = getBean(Banji.class);
		Ret ret = srv.save(0, banji);
		renderJson(ret); 
	}
	
	
	public void edit() {
		keepPara("p");	// 保持住分页的页号，便于在 ajax 提交后跳转到当前数据所在的页
		setAttr("banji", srv.edit(getParaToInt("id")));
		setAttr("collegeList", cosrv.getAll());
		render("add_edit.html");
	}

	/**
	 * 提交修改
	 */
	//@Before(MyProjectValidator.class)
	public void update() {
		Banji banji = getBean(Banji.class);
		Ret ret = srv.update(banji);
		renderJson(ret);
	}
	
	//删除
	public void delete() {
		Ret ret = srv.delete(getParaToInt("id"));
		renderJson(ret);
	}
}
